package com.walker.topic;

import com.rabbitmq.client.*;
import com.walker.BaseConfig;

/**
 * @Description:
 * @ProjectName: walker-step
 * @Package: com.walker.topic
 * @ClassName: TopicRecv02
 * @Author: yangyl
 * @CreateDate: 2020/6/21 15:31
 */
public class TopicRecv02 {

    public static final String EXCHANGE = "waring.exchange.03";

    public static ConnectionFactory connectionFactory() {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(BaseConfig.HOST);
        connectionFactory.setVirtualHost(BaseConfig.VHOST);
        connectionFactory.setPort(5672);
        connectionFactory.setUsername(BaseConfig.USERNAME);
        connectionFactory.setPassword(BaseConfig.PASSWORD);
        return connectionFactory;
    }

    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = connectionFactory();
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE, BuiltinExchangeType.TOPIC);
        channel.queueDeclare("QUEUE2",true,false,false,null);
        channel.queueBind("QUEUE2",EXCHANGE,"waring.*.*");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" +
                    delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume("QUEUE2", true, deliverCallback, consumerTag -> { });
    }

}
